package com.hhdys.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhdys.annotation.Access;
import com.hhdys.service.PositionService;

@Controller
@RequestMapping("/admin/position")
@Access("职位管理")
public class PositionAction {
	@Autowired
	private PositionService positionService;

	@RequestMapping("/list")
	@Access("查看")
	public String showPositions(Model model) {
		model.addAttribute("list", positionService.selPositions());
		return "admin/position/positionList";
	}

	@RequestMapping("/add")
	@ResponseBody
	@Access("添加")
	public String addPosition(String name) {
		Subject curUser = SecurityUtils.getSubject();
		if (positionService.addPosition(name, (String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/edit")
	@ResponseBody
	@Access("编辑")
	public String editPosition(String name, int id) {
		Subject curUser = SecurityUtils.getSubject();
		if (positionService.editPosition(id, name,
				(String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/del")
	@ResponseBody
	@Access("删除")
	public String delPosition(String ids) {
		if (positionService.delPosition(ids)) {
			return "success";
		}
		return "error";
	}

}
